package com.zhiming.item.service;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zhiming.common.exception.pojo.ExceptionEnum;
import com.zhiming.common.exception.pojo.ZmException;
import com.zhiming.item.mapper.AdvisoryMapper;
import com.zhiming.item.pojo.Advisory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户咨询 Service
 */

@Service
public class AdvisoryService {
    @Autowired(required = false)
    private AdvisoryMapper advisoryMapper;

    /**
     *新增咨询信息
     */
    public void saveAdvisory(Advisory advisory) {
        try {
            advisoryMapper.insert(advisory);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ZmException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     *删除咨询信息
     */
    public void deleteAdvisory(Integer id) {
        try {
            advisoryMapper.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ZmException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }

    /**
     *修改咨询信息
     */
    public void updateAdvisory(Advisory advisory) {
        try {
            advisoryMapper.updateById(advisory);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ZmException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    /**
     * 根据id查询咨询信息
     */
    public Advisory findById(Integer id) {
        Advisory advisory = advisoryMapper.selectById(id);
        if(advisory == null){
            throw new ZmException(ExceptionEnum.ADV_CATEGORY_NOT_FOUND);
        }
        return advisory;
    }

    /**
     * 查询所有信息
     */
    public List<Advisory> findAll() {
        List<Advisory> advisoryList = advisoryMapper.selectList(null);
        if(CollectionUtils.isEmpty(advisoryList)){
            throw new ZmException(ExceptionEnum.ADV_CATEGORY_NOT_FOUND);
        }
        return advisoryList;
    }
}
